
layui.use(['form'], function(){
    form=layui.form;
    form.on('select(brickType)', function(){
        let departmentid=$("#org option:selected").val().trim();
        var jsonA={
            'departmentid':departmentid
        }
        $.getJSON("/getPosition",jsonA,function(data){
            var optionstring = "";
            $.each(data,function(i,item){
                optionstring += "<option  value=\"" + item.positionid + "\"  name='"+item.sort+"'>" + item.name + "</option>";
            });
            $("#allOpisiton").html('<option value=""></option>' + optionstring);
            $("#positionid").val()
            form.render('select'); //这个很重要
        });
    });

    form.on('select(a)', function(){
        let positionid=$("#allOpisiton option:selected").val();
        let sort=$("#allOpisiton option:selected").attr("name");
        $("#positionid").val(positionid)
        $("#sort").val(sort);
        form.render('select'); //这个很重要
    });
});

function saveInfo() {
    let department = $("#org option:selected").text().trim();
    let RecruitmentType = $("#ty").val().trim();
    let numbers = $("#numbers").val().trim();
    let positionName = $("#allOpisiton option:selected").text().trim();
    let positionId = $("#positionid").val().trim();
    let positionSort = $("#sort").val().trim();
    let registrant = $("#registrant").val().trim();
    let expirationDate = $("#expirationDate").val().trim();
    let describe = $("#describe").val().trim();
    let require = $("#require").val().trim();
    let registranTime = $("#registrantTime").val().trim();

    if (department==null||RecruitmentType==null||numbers==null||positionName==null||positionId==null||positionSort==null||registrant==null||expirationDate==null||describe==null||require==null||registranTime==null){
        layer.msg("所有项目均不能为空")
        return
    }

    if (department==''||RecruitmentType==''||numbers==''||positionName==''||positionId==''||positionSort==''||registrant==''||expirationDate==''||describe==''||require==''||registranTime==''){
        layer.msg("所有项目均不能为空")
        return
    }

    let employment_dyb = {
        department: department,
        RecruitmentType: RecruitmentType,
        numbers: numbers,
        positionName: positionName,
        positionId: positionId,
        positionSort: positionSort,
        registrant: registrant,
        expirationDates: expirationDate,
        describe: describe,
        require: require,
        registranTimes: registranTime
    }
    console.log(employment_dyb)
    $.ajaxSettings.async = false;
    $.get('/savePositionInfo', employment_dyb, function (dataA) {
        if (dataA == '1') {
            alert("职业发布成功！")
            location.href = "http://localhost:8080/a";
        } else {
            alert("发布失败！")
        }
    })
    $.ajaxSettings.async = true;

}
    function upInfo(){
        let employmentId=$("#emp").val().trim()
        let department = $("#org option:selected").text().trim();
        let RecruitmentType = $("#ty").val().trim();
        let numbers = $("#numbers").val().trim();
        let positionName = $("#allOpisiton option:selected").text().trim();
        let positionId = $("#positionid").val().trim();
        let positionSort = $("#sort").val().trim();
        let registrant = $("#registrant").val().trim();
        let expirationDate = $("#expirationDate").val().trim();
        let describe = $("#describe").val().trim();
        let require = $("#require").val().trim();
        let registranTime = $("#registrantTime").val().trim();

        let employment_dyb = {
            department: department,
            RecruitmentType: RecruitmentType,
            numbers: numbers,
            positionName: positionName,
            positionId: positionId,
            positionSort: positionSort,
            registrant: registrant,
            expirationDates: expirationDate,
            describe: describe,
            require: require,
            registranTimes: registranTime,
            employmentId:employmentId
        }
        console.log(employment_dyb)
        $.ajaxSettings.async = false;
        $.get('/upPositionInfo', employment_dyb, function (dataA) {
            if (dataA == '1') {
                alert("职业修改成功！")
                location.href = "http://localhost:8080/a";
            } else {
                alert("发布失败！")
            }
        })
        $.ajaxSettings.async = true;

    }

    function yanzhengTime() {
        let date2 = new Date($("#expirationDate").val());
        date1=new Date().toLocaleDateString()
        date2=date2.getFullYear()+"/"+(date2.getMonth()+1)+"/"+date2.getDate()
        const getDaysDiffBetweenDates = (dateInitial, dateFinal) =>
            (dateFinal - dateInitial) / (1000 * 3600 * 24);

// 事例
        let daysDiffBetweenDates = getDaysDiffBetweenDates(new Date(date1),new Date(date2));
        console.log(daysDiffBetweenDates)
        if (daysDiffBetweenDates<0){
            $("#expirationDate").val('');
            layer.msg("截止日期不能早于当天，请重新填写")
            return
        }
        let registrantTime = new Date($("#registrantTime").val());
        registrantTime=registrantTime.getFullYear()+"/"+(registrantTime.getMonth()+1)+"/"+registrantTime.getDate()
        if (getDaysDiffBetweenDates(new Date(registrantTime),new Date(date2))<0){
            $("#expirationDate").val('');
            layer.msg("截止日期不能早于登记日期")
            return;
        }


    }
